package com.zbkj.service.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.user.Partner;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.service.dao.user.PartnerMapper;
import com.zbkj.service.service.user.PartnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 合作伙伴表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-11
 */
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements PartnerService {

    @Override
    public int getCountByIviteCode(String inviteCode) {
        if(StringUtils.isBlank(inviteCode)) {
            return 0;
        }
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getDeleteFlag, 0)
                .eq(Partner::getInviteCode, inviteCode)
                .last(" limit 1");
        return this.count(wrapper);
    }

    @Override
    public Partner getByIviteCode(String inviteCode) {
        if(StringUtils.isBlank(inviteCode)) {
            return null;
        }
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getDeleteFlag, 0)
                .eq(Partner::getInviteCode, inviteCode)
                .last(" limit 1");
        return this.getOne(wrapper,false);
    }

    @Override
    public PageInfo<Partner> adminGetlist(PageParamRequest queryRequest) {
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getDeleteFlag, 0)
                .orderByDesc(Partner::getId);
        Page<Partner> pageObj = PageHelper.startPage(queryRequest.getPage(), queryRequest.getLimit());
        List<Partner> list = this.list(wrapper);
        return CommonPage.copyPageInfo(pageObj,list);
    }

    @Override
    public Partner getPartnerById(Long id) {
        if(Objects.isNull(id)) {
            return null;
        }
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getDeleteFlag, 0)
                .eq(Partner::getId, id);
        return this.getOne(wrapper,false);
    }

    @Override
    public int getCountByPhone(String phone, Long id) {
        if(StringUtils.isBlank(phone)) {
            return 0;
        }
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getDeleteFlag, 0)
                .eq(Partner::getPhone, phone);
        if(Objects.nonNull(id)) {
            wrapper.ne(Partner::getId, id);
        }
        return this.count(wrapper);
    }
}
